package interfaces;

import java.io.IOException;
import java.nio.CharBuffer;
import java.util.Random;
import java.util.Scanner;

class RandomWords implements Readable {
    private static final char[] CHARS = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
            "abcdefghijklmnopqrstuvwxyz" +
            "0123456789" +
            "~./!@#$%^&*()_+|?>:").toCharArray();
    private static final int SIZE = CHARS.length;

    private int n;
    private final int c;
    private final Random random = new Random();

    public RandomWords(int c, int n) {
        this.c = c;
        this.n = n;
    }

    @Override
    public int read(CharBuffer cb) throws IOException {
        if(--n < 0)
            return -1;
        for(int i = 0; i < c; i ++)
            cb.append(CHARS[random.nextInt(SIZE)]);
        cb.append(" ");
        return n;
    }
}

public class ScannerDemo {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(new RandomWords(20, 5));
        while(scanner.hasNext())
            System.out.println(scanner.next());
    }
}
